{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Using TensorFlow backend.\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "from keras import backend as K\n",
    "from keras.layers import (Convolution2D, GlobalAveragePooling2D, Input, Lambda,InputLayer,\n",
    "                          MaxPooling2D, merge, Merge)\n",
    "from keras.layers.advanced_activations import LeakyReLU\n",
    "from keras.layers.normalization import BatchNormalization\n",
    "from keras.models import Model,Sequential\n",
    "from keras.regularizers import l2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [],
   "source": [
    "image_input = Input(shape=(416,416,3),name='input_1')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": true,
    "deletable": true,
    "editable": true
   },
   "outputs": [],
   "source": [
    "#先将ｗｅｉｇｈｔ读入这个变量，且扔掉开头四个数字\n",
    "weights_file = open('yolo.weights','rb')\n",
    "count = 16"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'\\x00\\x00\\x00\\x00\\x01\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00|\\xe8\\x01'"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 挨着增加层数且直接把ｗｅｉｇｈｔ附上\n",
    "#不使用ｃｏｕｎｔ\n",
    "weights_file.read(16)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/home/yh/.local/lib/python2.7/site-packages/ipykernel/__main__.py:26: UserWarning: Update your `Conv2D` call to the Keras 2 API: `Conv2D(32, (3, 3), name=\"conv2d_1\", activation=None, padding=\"same\", strides=(1, 1), weights=[array([[[..., kernel_regularizer=<keras.reg..., use_bias=False)`\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "file read to 3984\n"
     ]
    }
   ],
   "source": [
    "\"\"\"\n",
    "convolution_0\n",
    "batch_normalize=1\n",
    "filters=32\n",
    "size=3\n",
    "stride=1\n",
    "pad=1\n",
    "activation=leaky\n",
    "\"\"\"\n",
    "#read weights from yolo.weights\n",
    "#weights_file.seek(count)\n",
    "bias = np.ndarray(shape=(32,),dtype='float32',buffer=weights_file.read(32*4))\n",
    "bn_list = np.ndarray(shape=(3, 32),dtype='float32',buffer=weights_file.read(32*3*4))\n",
    "bn_weights = [bn_list[0], bias, bn_list[1], bn_list[2]]\n",
    "weights = np.ndarray(shape=(32,3,3,3),dtype='float32',buffer=weights_file.read(32*3*3*3*4))\n",
    "weights = np.transpose(weights,(2,3,1,0))\n",
    "\n",
    "#read for convolution\n",
    "tmp = Convolution2D(32,3,3,\n",
    "              subsample=(1,1),\n",
    "              border_mode='same',\n",
    "              activation=None, \n",
    "              weights=[weights], \n",
    "              bias=False,\n",
    "              W_regularizer=l2(0.0005),\n",
    "              name='conv2d_1')(image_input)\n",
    "\n",
    "#batchnormalization\n",
    "tmp = BatchNormalization(weights=bn_weights,name='batch_normalization_1')(tmp)\n",
    "                     \n",
    "#activation\n",
    "tmp = LeakyReLU(alpha=0.1,name='leakyrelu_1')(tmp)\n",
    "\n",
    "#help to go back. 因为file.seek(count)是默认从０开始算起到的位置\n",
    "count += (32 + 32*3 + 3*3*3*32)*4\n",
    "print \"file read to\",count"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/home/yh/.local/lib/python2.7/site-packages/ipykernel/__main__.py:6: UserWarning: Update your `MaxPooling2D` call to the Keras 2 API: `MaxPooling2D(padding=\"same\", strides=(2, 2), name=\"maxpooling2d_1\", pool_size=(2, 2))`\n"
     ]
    }
   ],
   "source": [
    "\"\"\"\n",
    "maxpool_0\n",
    "size=2\n",
    "stride=2\n",
    "\"\"\"\n",
    "tmp = MaxPooling2D(pool_size=(2,2),strides=(2,2),border_mode='same',name='maxpooling2d_1')(tmp)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "file read to 78736\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/home/yh/.local/lib/python2.7/site-packages/ipykernel/__main__.py:26: UserWarning: Update your `Conv2D` call to the Keras 2 API: `Conv2D(64, (3, 3), name=\"conv2d_2\", activation=None, padding=\"same\", strides=(1, 1), weights=[array([[[..., kernel_regularizer=<keras.reg..., use_bias=False)`\n"
     ]
    }
   ],
   "source": [
    "\"\"\"\n",
    "convolutional_1\n",
    "batch_normalize=1\n",
    "filters=64\n",
    "size=3\n",
    "stride=1\n",
    "pad=1\n",
    "activation=leaky\n",
    "\"\"\"\n",
    "#read weights from yolo.weights\n",
    "#weights_file.seek(count)\n",
    "bias = np.ndarray(shape=(64,),dtype='float32',buffer=weights_file.read(64*4))\n",
    "bn_list = np.ndarray(shape=(3, 64),dtype='float32',buffer=weights_file.read(64*3*4))\n",
    "bn_weights = [bn_list[0], bias, bn_list[1], bn_list[2]]\n",
    "weights = np.ndarray(shape=(64,32,3,3),dtype='float32',buffer=weights_file.read(64*32*3*3*4))\n",
    "weights = np.transpose(weights,(2,3,1,0))\n",
    "\n",
    "#read for convolution\n",
    "tmp = Convolution2D(64,3,3,\n",
    "              subsample=(1,1),\n",
    "              border_mode='same',\n",
    "              activation=None, \n",
    "              weights=[weights], \n",
    "              bias=False,\n",
    "              W_regularizer=l2(0.0005),\n",
    "              name='conv2d_2')(tmp)\n",
    "\n",
    "#batchnormalization\n",
    "tmp = BatchNormalization(weights=bn_weights,name='batch_normalization_2')(tmp)\n",
    "                     \n",
    "#activation\n",
    "tmp = LeakyReLU(alpha=0.1,name='leakyrelu_2')(tmp)\n",
    "\n",
    "#help to go back. 因为file.seek(count)是默认从０开始算起到的位置\n",
    "count += (64 + 64*3 + 3*3*64*32)*4\n",
    "print \"file read to\",count"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/home/yh/.local/lib/python2.7/site-packages/ipykernel/__main__.py:6: UserWarning: Update your `MaxPooling2D` call to the Keras 2 API: `MaxPooling2D(padding=\"same\", strides=(2, 2), name=\"maxpooling2d_2\", pool_size=(2, 2))`\n"
     ]
    }
   ],
   "source": [
    "\"\"\"\n",
    "maxpool_1\n",
    "size=2\n",
    "stride=2\n",
    "\"\"\"\n",
    "tmp = MaxPooling2D(pool_size=(2,2),strides=(2,2),border_mode='same',name='maxpooling2d_2')(tmp)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "file read to 375696\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/home/yh/.local/lib/python2.7/site-packages/ipykernel/__main__.py:26: UserWarning: Update your `Conv2D` call to the Keras 2 API: `Conv2D(128, (3, 3), name=\"conv2d_3\", activation=None, padding=\"same\", strides=(1, 1), weights=[array([[[..., kernel_regularizer=<keras.reg..., use_bias=False)`\n"
     ]
    }
   ],
   "source": [
    "\"\"\"\n",
    "convolutional_2\n",
    "batch_normalize=1\n",
    "filters=128\n",
    "size=3\n",
    "stride=1\n",
    "pad=1\n",
    "activation=leaky\n",
    "\"\"\"\n",
    "#read weights from yolo.weights\n",
    "#weights_file.seek(count)\n",
    "bias = np.ndarray(shape=(128,),dtype='float32',buffer=weights_file.read(128*4))\n",
    "bn_list = np.ndarray(shape=(3, 128),dtype='float32',buffer=weights_file.read(128*3*4))\n",
    "bn_weights = [bn_list[0], bias, bn_list[1], bn_list[2]]\n",
    "weights = np.ndarray(shape=(128,64,3,3),dtype='float32',buffer=weights_file.read(128*64*3*3*4))\n",
    "weights = np.transpose(weights,(2,3,1,0))\n",
    "\n",
    "#read for convolution\n",
    "tmp = Convolution2D(128,3,3,\n",
    "              subsample=(1,1),\n",
    "              border_mode='same',\n",
    "              activation=None, \n",
    "              weights=[weights], \n",
    "              bias=False,\n",
    "              W_regularizer=l2(0.0005),\n",
    "              name='conv2d_3')(tmp)\n",
    "\n",
    "#batchnormalization\n",
    "tmp = BatchNormalization(weights=bn_weights,name='batch_normalization_3')(tmp)\n",
    "                     \n",
    "#activation\n",
    "tmp = LeakyReLU(alpha=0.1,name='leakyrelu_3')(tmp)\n",
    "\n",
    "#help to go back. 因为file.seek(count)是默认从０开始算起到的位置\n",
    "count += (128 + 128*3 + 3*3*128*64)*4\n",
    "print \"file read to\",count"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "file read to 409488\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/home/yh/.local/lib/python2.7/site-packages/ipykernel/__main__.py:26: UserWarning: Update your `Conv2D` call to the Keras 2 API: `Conv2D(64, (1, 1), name=\"conv2d_4\", activation=None, padding=\"same\", strides=(1, 1), weights=[array([[[..., kernel_regularizer=<keras.reg..., use_bias=False)`\n"
     ]
    }
   ],
   "source": [
    "\"\"\"\n",
    "convolutional_3\n",
    "batch_normalize=1\n",
    "filters=64\n",
    "size=1\n",
    "stride=1\n",
    "pad=1\n",
    "activation=leaky\n",
    "\"\"\"\n",
    "#read weights from yolo.weights\n",
    "#weights_file.seek(count)\n",
    "bias = np.ndarray(shape=(64,),dtype='float32',buffer=weights_file.read(64*4))\n",
    "bn_list = np.ndarray(shape=(3, 64),dtype='float32',buffer=weights_file.read(64*3*4))\n",
    "bn_weights = [bn_list[0], bias, bn_list[1], bn_list[2]]\n",
    "weights = np.ndarray(shape=(64,128,1,1),dtype='float32',buffer=weights_file.read(64*128*1*1*4))\n",
    "weights = np.transpose(weights,(2,3,1,0))\n",
    "\n",
    "#read for convolution\n",
    "tmp = Convolution2D(64,1,1,\n",
    "              subsample=(1,1),\n",
    "              border_mode='same',\n",
    "              activation=None, \n",
    "              weights=[weights], \n",
    "              bias=False,\n",
    "              W_regularizer=l2(0.0005),\n",
    "              name='conv2d_4')(tmp)\n",
    "\n",
    "#batchnormalization\n",
    "tmp = BatchNormalization(weights=bn_weights,name='batch_normalization_4')(tmp)\n",
    "                     \n",
    "#activation\n",
    "tmp = LeakyReLU(alpha=0.1,name='leakyrelu_4')(tmp)\n",
    "\n",
    "#help to go back. 因为file.seek(count)是默认从０开始算起到的位置\n",
    "count += (64 + 64*3 + 1*1*64*128)*4\n",
    "print \"file read to\",count"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true,
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "file read to 706448\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/home/yh/.local/lib/python2.7/site-packages/ipykernel/__main__.py:26: UserWarning: Update your `Conv2D` call to the Keras 2 API: `Conv2D(128, (3, 3), name=\"conv2d_5\", activation=None, padding=\"same\", strides=(1, 1), weights=[array([[[..., kernel_regularizer=<keras.reg..., use_bias=False)`\n"
     ]
    }
   ],
   "source": [
    "\"\"\"\n",
    "convolutional_4\n",
    "batch_normalize=1\n",
    "filters=128\n",
    "size=3\n",
    "stride=1\n",
    "pad=1\n",
    "activation=leaky\n",
    "\"\"\"\n",
    "#read weights from yolo.weights\n",
    "#weights_file.seek(count)\n",
    "bias = np.ndarray(shape=(128,),dtype='float32',buffer=weights_file.read(128*4))\n",
    "bn_list = np.ndarray(shape=(3, 128),dtype='float32',buffer=weights_file.read(128*3*4))\n",
    "bn_weights = [bn_list[0], bias, bn_list[1], bn_list[2]]\n",
    "weights = np.ndarray(shape=(128,64,3,3),dtype='float32',buffer=weights_file.read(128*64*3*3*4))\n",
    "weights = np.transpose(weights,(2,3,1,0))\n",
    "\n",
    "#read for convolution\n",
    "tmp = Convolution2D(128,3,3,\n",
    "              subsample=(1,1),\n",
    "              border_mode='same',\n",
    "              activation=None, \n",
    "              weights=[weights], \n",
    "              bias=False,\n",
    "              W_regularizer=l2(0.0005),\n",
    "              name='conv2d_5')(tmp)\n",
    "\n",
    "#batchnormalization\n",
    "tmp = BatchNormalization(weights=bn_weights,name='batch_normalization_5')(tmp)\n",
    "                     \n",
    "#activation\n",
    "tmp = LeakyReLU(alpha=0.1,name='leakyrelu_5')(tmp)\n",
    "\n",
    "#help to go back. 因为file.seek(count)是默认从０开始算起到的位置\n",
    "count += (128 + 128*3 + 3*3*128*64)*4\n",
    "print \"file read to\",count"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/home/yh/.local/lib/python2.7/site-packages/ipykernel/__main__.py:6: UserWarning: Update your `MaxPooling2D` call to the Keras 2 API: `MaxPooling2D(padding=\"same\", strides=(2, 2), name=\"maxpooling2d_3\", pool_size=(2, 2))`\n"
     ]
    }
   ],
   "source": [
    "\"\"\"\n",
    "maxpool_2\n",
    "size=2\n",
    "stride=2\n",
    "\"\"\"\n",
    "tmp = MaxPooling2D(pool_size=(2,2),strides=(2,2),border_mode='same',name='maxpooling2d_3')(tmp)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "file read to 1890192\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/home/yh/.local/lib/python2.7/site-packages/ipykernel/__main__.py:26: UserWarning: Update your `Conv2D` call to the Keras 2 API: `Conv2D(256, (3, 3), name=\"conv2d_6\", activation=None, padding=\"same\", strides=(1, 1), weights=[array([[[..., kernel_regularizer=<keras.reg..., use_bias=False)`\n"
     ]
    }
   ],
   "source": [
    "\"\"\"\n",
    "convolutional_5\n",
    "batch_normalize=1\n",
    "filters=256\n",
    "size=3\n",
    "stride=1\n",
    "pad=1\n",
    "activation=leaky\n",
    "\"\"\"\n",
    "#read weights from yolo.weights\n",
    "#weights_file.seek(count)\n",
    "bias = np.ndarray(shape=(256,),dtype='float32',buffer=weights_file.read(256*4))\n",
    "bn_list = np.ndarray(shape=(3, 256),dtype='float32',buffer=weights_file.read(256*3*4))\n",
    "bn_weights = [bn_list[0], bias, bn_list[1], bn_list[2]]\n",
    "weights = np.ndarray(shape=(256,128,3,3),dtype='float32',buffer=weights_file.read(256*128*3*3*4))\n",
    "weights = np.transpose(weights,(2,3,1,0))\n",
    "\n",
    "#read for convolution\n",
    "tmp = Convolution2D(256,3,3,\n",
    "              subsample=(1,1),\n",
    "              border_mode='same',\n",
    "              activation=None, \n",
    "              weights=[weights], \n",
    "              bias=False,\n",
    "              W_regularizer=l2(0.0005),\n",
    "              name='conv2d_6')(tmp)\n",
    "\n",
    "#batchnormalization\n",
    "tmp = BatchNormalization(weights=bn_weights,name='batch_normalization_6')(tmp)\n",
    "                     \n",
    "#activation\n",
    "tmp = LeakyReLU(alpha=0.1,name='leakyrelu_6')(tmp)\n",
    "\n",
    "#help to go back. 因为file.seek(count)是默认从０开始算起到的位置\n",
    "count += (256 + 256*3 + 3*3*256*128)*4\n",
    "print \"file read to\",count"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "file read to 2023312\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/home/yh/.local/lib/python2.7/site-packages/ipykernel/__main__.py:26: UserWarning: Update your `Conv2D` call to the Keras 2 API: `Conv2D(128, (1, 1), name=\"conv2d_7\", activation=None, padding=\"same\", strides=(1, 1), weights=[array([[[..., kernel_regularizer=<keras.reg..., use_bias=False)`\n"
     ]
    }
   ],
   "source": [
    "\"\"\"\n",
    "convolutional_6\n",
    "batch_normalize=1\n",
    "filters=128\n",
    "size=1\n",
    "stride=1\n",
    "pad=1\n",
    "activation=leaky\n",
    "\"\"\"\n",
    "#read weights from yolo.weights\n",
    "#weights_file.seek(count)\n",
    "bias = np.ndarray(shape=(128,),dtype='float32',buffer=weights_file.read(128*4))\n",
    "bn_list = np.ndarray(shape=(3, 128),dtype='float32',buffer=weights_file.read(128*3*4))\n",
    "bn_weights = [bn_list[0], bias, bn_list[1], bn_list[2]]\n",
    "weights = np.ndarray(shape=(128,256,1,1),dtype='float32',buffer=weights_file.read(128*256*1*1*4))\n",
    "weights = np.transpose(weights,(2,3,1,0))\n",
    "\n",
    "#read for convolution\n",
    "tmp = Convolution2D(128,1,1,\n",
    "              subsample=(1,1),\n",
    "              border_mode='same',\n",
    "              activation=None, \n",
    "              weights=[weights], \n",
    "              bias=False,\n",
    "              W_regularizer=l2(0.0005),\n",
    "              name='conv2d_7')(tmp)\n",
    "\n",
    "#batchnormalization\n",
    "tmp = BatchNormalization(weights=bn_weights,name='batch_normalization_7')(tmp)\n",
    "                     \n",
    "#activation\n",
    "tmp = LeakyReLU(alpha=0.1,name='leakyrelu_7')(tmp)\n",
    "\n",
    "#help to go back. 因为file.seek(count)是默认从０开始算起到的位置\n",
    "count += (128 + 128*3 + 1*1*128*256)*4\n",
    "print \"file read to\",count"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "file read to 3207056\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/home/yh/.local/lib/python2.7/site-packages/ipykernel/__main__.py:26: UserWarning: Update your `Conv2D` call to the Keras 2 API: `Conv2D(256, (3, 3), name=\"conv2d_8\", activation=None, padding=\"same\", strides=(1, 1), weights=[array([[[..., kernel_regularizer=<keras.reg..., use_bias=False)`\n"
     ]
    }
   ],
   "source": [
    "\"\"\"\n",
    "convolutional_7\n",
    "batch_normalize=1\n",
    "filters=256\n",
    "size=3\n",
    "stride=1\n",
    "pad=1\n",
    "activation=leaky\n",
    "\"\"\"\n",
    "#read weights from yolo.weights\n",
    "#weights_file.seek(count)\n",
    "bias = np.ndarray(shape=(256,),dtype='float32',buffer=weights_file.read(256*4))\n",
    "bn_list = np.ndarray(shape=(3, 256),dtype='float32',buffer=weights_file.read(256*3*4))\n",
    "bn_weights = [bn_list[0], bias, bn_list[1], bn_list[2]]\n",
    "weights = np.ndarray(shape=(256,128,3,3),dtype='float32',buffer=weights_file.read(256*128*3*3*4))\n",
    "weights = np.transpose(weights,(2,3,1,0))\n",
    "\n",
    "#read for convolution\n",
    "tmp = Convolution2D(256,3,3,\n",
    "              subsample=(1,1),\n",
    "              border_mode='same',\n",
    "              activation=None, \n",
    "              weights=[weights], \n",
    "              bias=False,\n",
    "              W_regularizer=l2(0.0005),\n",
    "              name='conv2d_8')(tmp)\n",
    "\n",
    "#batchnormalization\n",
    "tmp = BatchNormalization(weights=bn_weights,name='batch_normalization_8')(tmp)\n",
    "                     \n",
    "#activation\n",
    "tmp = LeakyReLU(alpha=0.1,name='leakyrelu_8')(tmp)\n",
    "\n",
    "#help to go back. 因为file.seek(count)是默认从０开始算起到的位置\n",
    "count += (256 + 256*3 + 3*3*256*128)*4\n",
    "print \"file read to\",count"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/home/yh/.local/lib/python2.7/site-packages/ipykernel/__main__.py:6: UserWarning: Update your `MaxPooling2D` call to the Keras 2 API: `MaxPooling2D(padding=\"same\", strides=(2, 2), name=\"maxpooling2d_4\", pool_size=(2, 2))`\n"
     ]
    }
   ],
   "source": [
    "\"\"\"\n",
    "maxpool_3\n",
    "size=2\n",
    "stride=2\n",
    "\"\"\"\n",
    "tmp = MaxPooling2D(pool_size=(2,2),strides=(2,2),border_mode='same',name='maxpooling2d_4')(tmp)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "file read to 7933840\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/home/yh/.local/lib/python2.7/site-packages/ipykernel/__main__.py:26: UserWarning: Update your `Conv2D` call to the Keras 2 API: `Conv2D(512, (3, 3), name=\"conv2d_9\", activation=None, padding=\"same\", strides=(1, 1), weights=[array([[[..., kernel_regularizer=<keras.reg..., use_bias=False)`\n"
     ]
    }
   ],
   "source": [
    "\"\"\"\n",
    "convolutional_8\n",
    "batch_normalize=1\n",
    "filters=512\n",
    "size=3\n",
    "stride=1\n",
    "pad=1\n",
    "activation=leaky\n",
    "\"\"\"\n",
    "#read weights from yolo.weights\n",
    "#weights_file.seek(count)\n",
    "bias = np.ndarray(shape=(512,),dtype='float32',buffer=weights_file.read(512*4))\n",
    "bn_list = np.ndarray(shape=(3, 512),dtype='float32',buffer=weights_file.read(512*3*4))\n",
    "bn_weights = [bn_list[0], bias, bn_list[1], bn_list[2]]\n",
    "weights = np.ndarray(shape=(512,256,3,3),dtype='float32',buffer=weights_file.read(512*256*3*3*4))\n",
    "weights = np.transpose(weights,(2,3,1,0))\n",
    "\n",
    "#read for convolution\n",
    "tmp = Convolution2D(512,3,3,\n",
    "              subsample=(1,1),\n",
    "              border_mode='same',\n",
    "              activation=None, \n",
    "              weights=[weights], \n",
    "              bias=False,\n",
    "              W_regularizer=l2(0.0005),\n",
    "              name='conv2d_9')(tmp)\n",
    "\n",
    "#batchnormalization\n",
    "tmp = BatchNormalization(weights=bn_weights,name='batch_normalization_9')(tmp)\n",
    "                     \n",
    "#activation\n",
    "tmp = LeakyReLU(alpha=0.1,name='leakyrelu_9')(tmp)\n",
    "\n",
    "#help to go back. 因为file.seek(count)是默认从０开始算起到的位置\n",
    "count += (512 + 512*3 + 3*3*512*256)*4\n",
    "print \"file read to\",count"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "file read to 8462224\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/home/yh/.local/lib/python2.7/site-packages/ipykernel/__main__.py:26: UserWarning: Update your `Conv2D` call to the Keras 2 API: `Conv2D(256, (1, 1), name=\"conv2d_10\", activation=None, padding=\"same\", strides=(1, 1), weights=[array([[[..., kernel_regularizer=<keras.reg..., use_bias=False)`\n"
     ]
    }
   ],
   "source": [
    "\"\"\"\n",
    "convolutional_9\n",
    "batch_normalize=1\n",
    "filters=256\n",
    "size=1\n",
    "stride=1\n",
    "pad=1\n",
    "activation=leaky\n",
    "\"\"\"\n",
    "#read weights from yolo.weights\n",
    "#weights_file.seek(count)\n",
    "bias = np.ndarray(shape=(256,),dtype='float32',buffer=weights_file.read(256*4))\n",
    "bn_list = np.ndarray(shape=(3, 256),dtype='float32',buffer=weights_file.read(256*3*4))\n",
    "bn_weights = [bn_list[0], bias, bn_list[1], bn_list[2]]\n",
    "weights = np.ndarray(shape=(256,512,1,1),dtype='float32',buffer=weights_file.read(256*512*1*1*4))\n",
    "weights = np.transpose(weights,(2,3,1,0))\n",
    "\n",
    "#read for convolution\n",
    "tmp = Convolution2D(256,1,1,\n",
    "              subsample=(1,1),\n",
    "              border_mode='same',\n",
    "              activation=None, \n",
    "              weights=[weights], \n",
    "              bias=False,\n",
    "              W_regularizer=l2(0.0005),\n",
    "              name='conv2d_10')(tmp)\n",
    "\n",
    "#batchnormalization\n",
    "tmp = BatchNormalization(weights=bn_weights,name='batch_normalization_10')(tmp)\n",
    "                     \n",
    "#activation\n",
    "tmp = LeakyReLU(alpha=0.1,name='leakyrelu_10')(tmp)\n",
    "\n",
    "#help to go back. 因为file.seek(count)是默认从０开始算起到的位置\n",
    "count += (256 + 256*3 + 1*1*256*512)*4\n",
    "print \"file read to\",count"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "file read to 13189008\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/home/yh/.local/lib/python2.7/site-packages/ipykernel/__main__.py:26: UserWarning: Update your `Conv2D` call to the Keras 2 API: `Conv2D(512, (3, 3), name=\"conv2d_11\", activation=None, padding=\"same\", strides=(1, 1), weights=[array([[[..., kernel_regularizer=<keras.reg..., use_bias=False)`\n"
     ]
    }
   ],
   "source": [
    "\"\"\"\n",
    "convolutional_10\n",
    "batch_normalize=1\n",
    "filters=512\n",
    "size=3\n",
    "stride=1\n",
    "pad=1\n",
    "activation=leaky\n",
    "\"\"\"\n",
    "#read weights from yolo.weights\n",
    "#weights_file.seek(count)\n",
    "bias = np.ndarray(shape=(512,),dtype='float32',buffer=weights_file.read(512*4))\n",
    "bn_list = np.ndarray(shape=(3, 512),dtype='float32',buffer=weights_file.read(512*3*4))\n",
    "bn_weights = [bn_list[0], bias, bn_list[1], bn_list[2]]\n",
    "weights = np.ndarray(shape=(512,256,3,3),dtype='float32',buffer=weights_file.read(512*256*3*3*4))\n",
    "weights = np.transpose(weights,(2,3,1,0))\n",
    "\n",
    "#read for convolution\n",
    "tmp = Convolution2D(512,3,3,\n",
    "              subsample=(1,1),\n",
    "              border_mode='same',\n",
    "              activation=None, \n",
    "              weights=[weights], \n",
    "              bias=False,\n",
    "              W_regularizer=l2(0.0005),\n",
    "              name='conv2d_11')(tmp)\n",
    "\n",
    "#batchnormalization\n",
    "tmp = BatchNormalization(weights=bn_weights,name='batch_normalization_11')(tmp)\n",
    "                     \n",
    "#activation\n",
    "tmp = LeakyReLU(alpha=0.1,name='leakyrelu_11')(tmp)\n",
    "\n",
    "#help to go back. 因为file.seek(count)是默认从０开始算起到的位置\n",
    "count += (512 + 512*3 + 3*3*512*256)*4\n",
    "print \"file read to\",count"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "file read to 13717392\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/home/yh/.local/lib/python2.7/site-packages/ipykernel/__main__.py:26: UserWarning: Update your `Conv2D` call to the Keras 2 API: `Conv2D(256, (1, 1), name=\"conv2d_12\", activation=None, padding=\"same\", strides=(1, 1), weights=[array([[[..., kernel_regularizer=<keras.reg..., use_bias=False)`\n"
     ]
    }
   ],
   "source": [
    "\"\"\"\n",
    "convolutional_11\n",
    "batch_normalize=1\n",
    "filters=256\n",
    "size=3\n",
    "stride=1\n",
    "pad=1\n",
    "activation=leaky\n",
    "\"\"\"\n",
    "#read weights from yolo.weights\n",
    "#weights_file.seek(count)\n",
    "bias = np.ndarray(shape=(256,),dtype='float32',buffer=weights_file.read(256*4))\n",
    "bn_list = np.ndarray(shape=(3, 256),dtype='float32',buffer=weights_file.read(256*3*4))\n",
    "bn_weights = [bn_list[0], bias, bn_list[1], bn_list[2]]\n",
    "weights = np.ndarray(shape=(256,512,1,1),dtype='float32',buffer=weights_file.read(256*512*1*1*4))\n",
    "weights = np.transpose(weights,(2,3,1,0))\n",
    "\n",
    "#read for convolution\n",
    "tmp = Convolution2D(256,1,1,\n",
    "              subsample=(1,1),\n",
    "              border_mode='same',\n",
    "              activation=None, \n",
    "              weights=[weights], \n",
    "              bias=False,\n",
    "              W_regularizer=l2(0.0005),\n",
    "              name='conv2d_12')(tmp)\n",
    "\n",
    "#batchnormalization\n",
    "tmp = BatchNormalization(weights=bn_weights,name='batch_normalization_12')(tmp)\n",
    "                     \n",
    "#activation\n",
    "tmp = LeakyReLU(alpha=0.1,name='leakyrelu_12')(tmp)\n",
    "\n",
    "#help to go back. 因为file.seek(count)是默认从０开始算起到的位置\n",
    "count += (256 + 256*3 + 1*1*256*512)*4\n",
    "print \"file read to\",count"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/home/yh/.local/lib/python2.7/site-packages/ipykernel/__main__.py:26: UserWarning: Update your `Conv2D` call to the Keras 2 API: `Conv2D(512, (3, 3), name=\"conv2d_13\", activation=None, padding=\"same\", strides=(1, 1), weights=[array([[[..., kernel_regularizer=<keras.reg..., use_bias=False)`\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "file read to 18444176\n"
     ]
    }
   ],
   "source": [
    "\"\"\"\n",
    "convolutional_12\n",
    "batch_normalize=1\n",
    "filters=512\n",
    "size=3\n",
    "stride=1\n",
    "pad=1\n",
    "activation=leaky\n",
    "\"\"\"\n",
    "#read weights from yolo.weights\n",
    "#weights_file.seek(count)\n",
    "bias = np.ndarray(shape=(512,),dtype='float32',buffer=weights_file.read(512*4))\n",
    "bn_list = np.ndarray(shape=(3, 512),dtype='float32',buffer=weights_file.read(512*3*4))\n",
    "bn_weights = [bn_list[0], bias, bn_list[1], bn_list[2]]\n",
    "weights = np.ndarray(shape=(512,256,3,3),dtype='float32',buffer=weights_file.read(512*256*3*3*4))\n",
    "weights = np.transpose(weights,(2,3,1,0))\n",
    "\n",
    "#read for convolution\n",
    "tmp = Convolution2D(512,3,3,\n",
    "              subsample=(1,1),\n",
    "              border_mode='same',\n",
    "              activation=None, \n",
    "              weights=[weights], \n",
    "              bias=False,\n",
    "              W_regularizer=l2(0.0005),\n",
    "              name='conv2d_13')(tmp)\n",
    "\n",
    "#batchnormalization\n",
    "tmp = BatchNormalization(weights=bn_weights,name='batch_normalization_13')(tmp)\n",
    "                     \n",
    "#activation\n",
    "image_tmp_output = LeakyReLU(alpha=0.1,name='leakyrelu_13')(tmp)\n",
    "\n",
    "#help to go back. 因为file.seek(count)是默认从０开始算起到的位置\n",
    "count += (512 + 512*3 + 3*3*512*256)*4\n",
    "print \"file read to\",count"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/home/yh/.local/lib/python2.7/site-packages/ipykernel/__main__.py:6: UserWarning: Update your `MaxPooling2D` call to the Keras 2 API: `MaxPooling2D(padding=\"same\", strides=(2, 2), name=\"maxpooling2d_5\", pool_size=(2, 2))`\n"
     ]
    }
   ],
   "source": [
    "\"\"\"\n",
    "maxpool_4\n",
    "size=2\n",
    "stride=2\n",
    "\"\"\"\n",
    "tmp = MaxPooling2D(pool_size=(2,2),strides=(2,2),border_mode='same',name='maxpooling2d_5')(image_tmp_output)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/home/yh/.local/lib/python2.7/site-packages/ipykernel/__main__.py:26: UserWarning: Update your `Conv2D` call to the Keras 2 API: `Conv2D(1024, (3, 3), name=\"conv2d_14\", activation=None, padding=\"same\", strides=(1, 1), weights=[array([[[..., kernel_regularizer=<keras.reg..., use_bias=False)`\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "file read to 37334928\n"
     ]
    }
   ],
   "source": [
    "\"\"\"\n",
    "convolutional_13\n",
    "batch_normalize=1\n",
    "filters=1024\n",
    "size=3\n",
    "stride=1\n",
    "pad=1\n",
    "activation=leaky\n",
    "\"\"\"\n",
    "#read weights from yolo.weights\n",
    "#weights_file.seek(count)\n",
    "bias = np.ndarray(shape=(1024,),dtype='float32',buffer=weights_file.read(1024*4))\n",
    "bn_list = np.ndarray(shape=(3, 1024),dtype='float32',buffer=weights_file.read(1024*3*4))\n",
    "bn_weights = [bn_list[0], bias, bn_list[1], bn_list[2]]\n",
    "weights = np.ndarray(shape=(1024,512,3,3),dtype='float32',buffer=weights_file.read(1024*512*3*3*4))\n",
    "weights = np.transpose(weights,(2,3,1,0))\n",
    "\n",
    "#read for convolution\n",
    "tmp = Convolution2D(1024,3,3,\n",
    "              subsample=(1,1),\n",
    "              border_mode='same',\n",
    "              activation=None, \n",
    "              weights=[weights], \n",
    "              bias=False,\n",
    "              W_regularizer=l2(0.0005),\n",
    "              name='conv2d_14')(tmp)\n",
    "\n",
    "#batchnormalization\n",
    "tmp = BatchNormalization(weights=bn_weights,name='batch_normalization_14')(tmp)\n",
    "                     \n",
    "#activation\n",
    "tmp = LeakyReLU(alpha=0.1,name='leakyrelu_14')(tmp)\n",
    "#help to go back. 因为file.seek(count)是默认从０开始算起到的位置\n",
    "count += (1024 + 1024*3 + 3*3*1024*512)*4\n",
    "print \"file read to\",count"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "file read to 39440272\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/home/yh/.local/lib/python2.7/site-packages/ipykernel/__main__.py:26: UserWarning: Update your `Conv2D` call to the Keras 2 API: `Conv2D(512, (1, 1), name=\"conv2d_15\", activation=None, padding=\"same\", strides=(1, 1), weights=[array([[[..., kernel_regularizer=<keras.reg..., use_bias=False)`\n"
     ]
    }
   ],
   "source": [
    "\"\"\"\n",
    "convolutional_14\n",
    "batch_normalize=1\n",
    "filters=512\n",
    "size=3\n",
    "stride=1\n",
    "pad=1\n",
    "activation=leaky\n",
    "\"\"\"\n",
    "#read weights from yolo.weights\n",
    "#weights_file.seek(count)\n",
    "bias = np.ndarray(shape=(512,),dtype='float32',buffer=weights_file.read(512*4))\n",
    "bn_list = np.ndarray(shape=(3, 512),dtype='float32',buffer=weights_file.read(512*3*4))\n",
    "bn_weights = [bn_list[0], bias, bn_list[1], bn_list[2]]\n",
    "weights = np.ndarray(shape=(512,1024,1,1),dtype='float32',buffer=weights_file.read(512*1024*1*1*4))\n",
    "weights = np.transpose(weights,(2,3,1,0))\n",
    "\n",
    "#read for convolution\n",
    "tmp = Convolution2D(512,1,1,\n",
    "              subsample=(1,1),\n",
    "              border_mode='same',\n",
    "              activation=None, \n",
    "              weights=[weights], \n",
    "              bias=False,\n",
    "              W_regularizer=l2(0.0005),\n",
    "              name='conv2d_15')(tmp)\n",
    "\n",
    "#batchnormalization\n",
    "tmp = BatchNormalization(weights=bn_weights,name='batch_normalization_15')(tmp)\n",
    "                     \n",
    "#activation\n",
    "tmp = LeakyReLU(alpha=0.1,name='leakyrelu_15')(tmp)\n",
    "\n",
    "#help to go back. 因为file.seek(count)是默认从０开始算起到的位置\n",
    "count += (512 + 512*3 + 1*1*512*1024)*4\n",
    "print \"file read to\",count"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/home/yh/.local/lib/python2.7/site-packages/ipykernel/__main__.py:26: UserWarning: Update your `Conv2D` call to the Keras 2 API: `Conv2D(1024, (3, 3), name=\"conv2d_16\", activation=None, padding=\"same\", strides=(1, 1), weights=[array([[[..., kernel_regularizer=<keras.reg..., use_bias=False)`\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "file read to 58331024\n"
     ]
    }
   ],
   "source": [
    "\"\"\"\n",
    "convolutional_15\n",
    "batch_normalize=1\n",
    "filters=1024\n",
    "size=3\n",
    "stride=1\n",
    "pad=1\n",
    "activation=leaky\n",
    "\"\"\"\n",
    "#read weights from yolo.weights\n",
    "#weights_file.seek(count)\n",
    "bias = np.ndarray(shape=(1024,),dtype='float32',buffer=weights_file.read(1024*4))\n",
    "bn_list = np.ndarray(shape=(3, 1024),dtype='float32',buffer=weights_file.read(1024*3*4))\n",
    "bn_weights = [bn_list[0], bias, bn_list[1], bn_list[2]]\n",
    "weights = np.ndarray(shape=(1024,512,3,3),dtype='float32',buffer=weights_file.read(1024*512*3*3*4))\n",
    "weights = np.transpose(weights,(2,3,1,0))\n",
    "\n",
    "#read for convolution\n",
    "tmp = Convolution2D(1024,3,3,\n",
    "              subsample=(1,1),\n",
    "              border_mode='same',\n",
    "              activation=None, \n",
    "              weights=[weights], \n",
    "              bias=False,\n",
    "              W_regularizer=l2(0.0005),\n",
    "              name='conv2d_16')(tmp)\n",
    "\n",
    "#batchnormalization\n",
    "tmp = BatchNormalization(weights=bn_weights,name='batch_normalization_16')(tmp)\n",
    "                     \n",
    "#activation\n",
    "tmp = LeakyReLU(alpha=0.1,name='leakyrelu_16')(tmp)\n",
    "\n",
    "#help to go back. 因为file.seek(count)是默认从０开始算起到的位置\n",
    "count += (1024 + 1024*3 + 3*3*1024*512)*4\n",
    "print \"file read to\",count"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "file read to 60436368\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/home/yh/.local/lib/python2.7/site-packages/ipykernel/__main__.py:26: UserWarning: Update your `Conv2D` call to the Keras 2 API: `Conv2D(512, (1, 1), name=\"conv2d_17\", activation=None, padding=\"same\", strides=(1, 1), weights=[array([[[..., kernel_regularizer=<keras.reg..., use_bias=False)`\n"
     ]
    }
   ],
   "source": [
    "\"\"\"\n",
    "convolutional_16\n",
    "batch_normalize=1\n",
    "filters=512\n",
    "size=3\n",
    "stride=1\n",
    "pad=1\n",
    "activation=leaky\n",
    "\"\"\"\n",
    "#read weights from yolo.weights\n",
    "#weights_file.seek(count)\n",
    "bias = np.ndarray(shape=(512,),dtype='float32',buffer=weights_file.read(512*4))\n",
    "bn_list = np.ndarray(shape=(3, 512),dtype='float32',buffer=weights_file.read(512*3*4))\n",
    "bn_weights = [bn_list[0], bias, bn_list[1], bn_list[2]]\n",
    "weights = np.ndarray(shape=(512,1024,1,1),dtype='float32',buffer=weights_file.read(512*1024*1*1*4))\n",
    "weights = np.transpose(weights,(2,3,1,0))\n",
    "\n",
    "#read for convolution\n",
    "tmp = Convolution2D(512,1,1,\n",
    "              subsample=(1,1),\n",
    "              border_mode='same',\n",
    "              activation=None, \n",
    "              weights=[weights], \n",
    "              bias=False,\n",
    "              W_regularizer=l2(0.0005),\n",
    "              name='conv2d_17')(tmp)\n",
    "\n",
    "#batchnormalization\n",
    "tmp = BatchNormalization(weights=bn_weights,name='batch_normalization_17')(tmp)\n",
    "                     \n",
    "#activation\n",
    "tmp = LeakyReLU(alpha=0.1,name='leakyrelu_17')(tmp)\n",
    "\n",
    "#help to go back. 因为file.seek(count)是默认从０开始算起到的位置\n",
    "count += (512 + 512*3 + 1*1*512*1024)*4\n",
    "print \"file read to\",count"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/home/yh/.local/lib/python2.7/site-packages/ipykernel/__main__.py:26: UserWarning: Update your `Conv2D` call to the Keras 2 API: `Conv2D(1024, (3, 3), name=\"conv2d_18\", activation=None, padding=\"same\", strides=(1, 1), weights=[array([[[..., kernel_regularizer=<keras.reg..., use_bias=False)`\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "file read to 79327120\n"
     ]
    }
   ],
   "source": [
    "\"\"\"\n",
    "convolutional_17\n",
    "batch_normalize=1\n",
    "filters=1024\n",
    "size=3\n",
    "stride=1\n",
    "pad=1\n",
    "activation=leaky\n",
    "\"\"\"\n",
    "#read weights from yolo.weights\n",
    "#weights_file.seek(count)\n",
    "bias = np.ndarray(shape=(1024,),dtype='float32',buffer=weights_file.read(1024*4))\n",
    "bn_list = np.ndarray(shape=(3, 1024),dtype='float32',buffer=weights_file.read(1024*3*4))\n",
    "bn_weights = [bn_list[0], bias, bn_list[1], bn_list[2]]\n",
    "weights = np.ndarray(shape=(1024,512,3,3),dtype='float32',buffer=weights_file.read(1024*512*3*3*4))\n",
    "weights = np.transpose(weights,(2,3,1,0))\n",
    "\n",
    "#read for convolution\n",
    "tmp = Convolution2D(1024,3,3,\n",
    "              subsample=(1,1),\n",
    "              border_mode='same',\n",
    "              activation=None, \n",
    "              weights=[weights], \n",
    "              bias=False,\n",
    "              W_regularizer=l2(0.0005),\n",
    "              name='conv2d_18')(tmp)\n",
    "\n",
    "#batchnormalization\n",
    "tmp = BatchNormalization(weights=bn_weights,name='batch_normalization_18')(tmp)\n",
    "                     \n",
    "#activation\n",
    "tmp = LeakyReLU(alpha=0.1,name='leakyrelu_18')(tmp)\n",
    "\n",
    "#help to go back. 因为file.seek(count)是默认从０开始算起到的位置\n",
    "count += (1024 + 1024*3 + 3*3*1024*512)*4\n",
    "print \"file read to\",count"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/home/yh/.local/lib/python2.7/site-packages/ipykernel/__main__.py:26: UserWarning: Update your `Conv2D` call to the Keras 2 API: `Conv2D(1024, (3, 3), name=\"conv2d_19\", activation=None, padding=\"same\", strides=(1, 1), weights=[array([[[..., kernel_regularizer=<keras.reg..., use_bias=False)`\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "file read to 117092240\n"
     ]
    }
   ],
   "source": [
    "\"\"\"\n",
    "convolutional_18\n",
    "batch_normalize=1\n",
    "filters=1024\n",
    "size=3\n",
    "stride=1\n",
    "pad=1\n",
    "activation=leaky\n",
    "\"\"\"\n",
    "#read weights from yolo.weights\n",
    "#weights_file.seek(count)\n",
    "bias = np.ndarray(shape=(1024,),dtype='float32',buffer=weights_file.read(1024*4))\n",
    "bn_list = np.ndarray(shape=(3, 1024),dtype='float32',buffer=weights_file.read(1024*3*4))\n",
    "bn_weights = [bn_list[0], bias, bn_list[1], bn_list[2]]\n",
    "weights = np.ndarray(shape=(1024,1024,3,3),dtype='float32',buffer=weights_file.read(1024*1024*3*3*4))\n",
    "weights = np.transpose(weights,(2,3,1,0))\n",
    "\n",
    "#read for convolution\n",
    "tmp = Convolution2D(1024,3,3,\n",
    "              subsample=(1,1),\n",
    "              border_mode='same',\n",
    "              activation=None, \n",
    "              weights=[weights], \n",
    "              bias=False,\n",
    "              W_regularizer=l2(0.0005),\n",
    "              name='conv2d_19')(tmp)\n",
    "\n",
    "#batchnormalization\n",
    "tmp = BatchNormalization(weights=bn_weights,name='batch_normalization_19')(tmp)\n",
    "                     \n",
    "#activation\n",
    "tmp = LeakyReLU(alpha=0.1,name='leakyrelu_19')(tmp)\n",
    "\n",
    "#help to go back. 因为file.seek(count)是默认从０开始算起到的位置\n",
    "count += (1024 + 1024*3 + 3*3*1024*1024)*4\n",
    "print \"file read to\",count"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/home/yh/.local/lib/python2.7/site-packages/ipykernel/__main__.py:26: UserWarning: Update your `Conv2D` call to the Keras 2 API: `Conv2D(1024, (3, 3), name=\"conv2d_20\", activation=None, padding=\"same\", strides=(1, 1), weights=[array([[[..., kernel_regularizer=<keras.reg..., use_bias=False)`\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "file read to 154857360\n"
     ]
    }
   ],
   "source": [
    "\"\"\"\n",
    "convolutional_19\n",
    "batch_normalize=1\n",
    "filters=1024\n",
    "size=3\n",
    "stride=1\n",
    "pad=1\n",
    "activation=leaky\n",
    "\"\"\"\n",
    "#read weights from yolo.weights\n",
    "#weights_file.seek(count)\n",
    "bias = np.ndarray(shape=(1024,),dtype='float32',buffer=weights_file.read(1024*4))\n",
    "bn_list = np.ndarray(shape=(3, 1024),dtype='float32',buffer=weights_file.read(1024*3*4))\n",
    "bn_weights = [bn_list[0], bias, bn_list[1], bn_list[2]]\n",
    "weights = np.ndarray(shape=(1024,1024,3,3),dtype='float32',buffer=weights_file.read(1024*1024*3*3*4))\n",
    "weights = np.transpose(weights,(2,3,1,0))\n",
    "\n",
    "#read for convolution\n",
    "tmp = Convolution2D(1024,3,3,\n",
    "              subsample=(1,1),\n",
    "              border_mode='same',\n",
    "              activation=None, \n",
    "              weights=[weights], \n",
    "              bias=False,\n",
    "              W_regularizer=l2(0.0005),\n",
    "              name='conv2d_20')(tmp)\n",
    "\n",
    "#batchnormalization\n",
    "tmp = BatchNormalization(weights=bn_weights,name='batch_normalization_20')(tmp)\n",
    "                     \n",
    "#activation\n",
    "tmp = LeakyReLU(alpha=0.1,name='leakyrelu_20')(tmp)\n",
    "\n",
    "#help to go back. 因为file.seek(count)是默认从０开始算起到的位置\n",
    "count += (1024 + 1024*3 + 3*3*1024*1024)*4\n",
    "print \"file read to\",count"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "file read to 154989456\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/home/yh/.local/lib/python2.7/site-packages/ipykernel/__main__.py:27: UserWarning: Update your `Conv2D` call to the Keras 2 API: `Conv2D(64, (1, 1), name=\"conv2d_21\", activation=None, padding=\"same\", strides=(1, 1), weights=[array([[[..., kernel_regularizer=<keras.reg..., use_bias=False)`\n"
     ]
    }
   ],
   "source": [
    "\"\"\"\n",
    "分叉处\n",
    "convolutional_20\n",
    "batch_normalize=1\n",
    "filters=64\n",
    "size=3\n",
    "stride=1\n",
    "pad=1\n",
    "activation=leaky\n",
    "\"\"\"\n",
    "#read weights from yolo.weights\n",
    "#weights_file.seek(count)\n",
    "bias = np.ndarray(shape=(64,),dtype='float32',buffer=weights_file.read(64*4))\n",
    "bn_list = np.ndarray(shape=(3, 64),dtype='float32',buffer=weights_file.read(64*3*4))\n",
    "bn_weights = [bn_list[0], bias, bn_list[1], bn_list[2]]\n",
    "weights = np.ndarray(shape=(64,512,1,1),dtype='float32',buffer=weights_file.read(64*512*1*1*4))\n",
    "weights = np.transpose(weights,(2,3,1,0))\n",
    "\n",
    "#read for convolution\n",
    "tmp2 = Convolution2D(64,1,1,\n",
    "              subsample=(1,1),\n",
    "              border_mode='same',\n",
    "              activation=None, \n",
    "              weights=[weights], \n",
    "              bias=False,\n",
    "              W_regularizer=l2(0.0005),\n",
    "              name='conv2d_21')(image_tmp_output)\n",
    "\n",
    "#batchnormalization\n",
    "tmp2 = BatchNormalization(weights=bn_weights,name='batch_normalization_21')(tmp2)\n",
    "                     \n",
    "#activation\n",
    "tmp2 = LeakyReLU(alpha=0.1,name='leakyrelu_21')(tmp2)\n",
    "\n",
    "#help to go back. 因为file.seek(count)是默认从０开始算起到的位置\n",
    "count += (64 + 64*3 + 1*1*64*512)*4\n",
    "print \"file read to\",count"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/home/yh/.local/lib/python2.7/site-packages/ipykernel/__main__.py:6: UserWarning: The `Merge` layer is deprecated and will be removed after 08/2017. Use instead layers from `keras.layers.merge`, e.g. `add`, `concatenate`, etc.\n"
     ]
    }
   ],
   "source": [
    "#需要和之前的image_tmp_output融合了\n",
    "def fun(x):\n",
    "    import tensorflow as tf\n",
    "    return tf.space_to_depth(x, block_size=2)\n",
    "tmp2 = Lambda(fun,output_shape=(13,13,256),name='space_to_depth_2')(tmp2)\n",
    "tmp = Merge(name='merge_1',mode='concat')([tmp2,tmp])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/home/yh/.local/lib/python2.7/site-packages/ipykernel/__main__.py:26: UserWarning: Update your `Conv2D` call to the Keras 2 API: `Conv2D(1024, (3, 3), name=\"conv2d_22\", activation=None, padding=\"same\", strides=(1, 1), weights=[array([[[..., kernel_regularizer=<keras.reg..., use_bias=False)`\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "file read to 202191760\n"
     ]
    }
   ],
   "source": [
    "\"\"\"\n",
    "convolutional_21\n",
    "batch_normalize=1\n",
    "filters=1024\n",
    "size=3\n",
    "stride=1\n",
    "pad=1\n",
    "activation=leaky\n",
    "\"\"\"\n",
    "#read weights from yolo.weights\n",
    "#weights_file.seek(count)\n",
    "bias = np.ndarray(shape=(1024,),dtype='float32',buffer=weights_file.read(1024*4))\n",
    "bn_list = np.ndarray(shape=(3, 1024),dtype='float32',buffer=weights_file.read(1024*3*4))\n",
    "bn_weights = [bn_list[0], bias, bn_list[1], bn_list[2]]\n",
    "weights = np.ndarray(shape=(1024,1280,3,3),dtype='float32',buffer=weights_file.read(1024*1280*3*3*4))\n",
    "weights = np.transpose(weights,(2,3,1,0))\n",
    "\n",
    "#read for convolution\n",
    "tmp = Convolution2D(1024,3,3,\n",
    "              subsample=(1,1),\n",
    "              border_mode='same',\n",
    "              activation=None, \n",
    "              weights=[weights], \n",
    "              bias=False,\n",
    "              W_regularizer=l2(0.0005),\n",
    "              name='conv2d_22')(tmp)\n",
    "\n",
    "#batchnormalization\n",
    "tmp = BatchNormalization(weights=bn_weights,name='batch_normalization_22')(tmp)\n",
    "                     \n",
    "#activation\n",
    "tmp = LeakyReLU(alpha=0.1,name='leakyrelu_22')(tmp)\n",
    "\n",
    "#help to go back. 因为file.seek(count)是默认从０开始算起到的位置\n",
    "count += (1024 + 1024*3 + 3*3*1024*1280)*4\n",
    "print \"file read to\",count"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "file read to 203932560\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/home/yh/.local/lib/python2.7/site-packages/ipykernel/__main__.py:24: UserWarning: Update your `Conv2D` call to the Keras 2 API: `Conv2D(425, (1, 1), name=\"conv2d_23\", activation=None, padding=\"same\", strides=(1, 1), weights=[array([[[..., kernel_regularizer=<keras.reg..., use_bias=True)`\n"
     ]
    }
   ],
   "source": [
    "\"\"\"\n",
    "convolutional_22\n",
    "batch_normalize=1\n",
    "filters=425\n",
    "size=1\n",
    "stride=1\n",
    "pad=1\n",
    "activation=leaky\n",
    "\"\"\"\n",
    "#read weights from yolo.weights\n",
    "#weights_file.seek(count)\n",
    "bias = np.ndarray(shape=(425,),dtype='float32',buffer=weights_file.read(425*4))\n",
    "weights = np.ndarray(shape=(425,1024,1,1),dtype='float32',buffer=weights_file.read(425*1024*1*1*4))\n",
    "weights = np.transpose(weights,(2,3,1,0))\n",
    "\n",
    "#read for convolution\n",
    "tmp = Convolution2D(425,1,1,\n",
    "              subsample=(1,1),\n",
    "              border_mode='same',\n",
    "              activation=None, \n",
    "              weights=[weights,bias], \n",
    "              bias=True,\n",
    "              W_regularizer=l2(0.0005),\n",
    "              name='conv2d_23')(tmp)\n",
    "\n",
    "#help to go back. 因为file.seek(count)是默认从０开始算起到的位置\n",
    "count += (1*1*1024*425)*4\n",
    "print \"file read to\",count"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [],
   "source": [
    "model = Model(inputs=image_input, outputs=tmp)\n",
    "model.save('model_data/model.h5')\n",
    "weights_file.close()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [],
   "source": [
    "from keras.utils.vis_utils import plot_model\n",
    "plot_model(model,to_file='model_data/model.png',show_shapes=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "metadata": {
    "collapsed": true,
    "deletable": true,
    "editable": true
   },
   "outputs": [],
   "source": [
    "weight_file = open('../yad2k/yolo.weights')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true,
    "deletable": true,
    "editable": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 2",
   "language": "python",
   "name": "python2"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.12+"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
